package edu.nwpu.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import edu.nwpu.pojo.FoodCount;
import edu.nwpu.pojo.Order;
import edu.nwpu.service.FoodCountService;
import edu.nwpu.service.OrderService;
import edu.nwpu.utils.OrderUtil;
import edu.nwpu.utils.ResponseUtil;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class OrderController {

    @Resource
    private OrderService orderService;

    @ApiOperation(value = "根据订单id查询订单", httpMethod = "GET", response = String.class,
            notes = "返回json")
    @GetMapping("/order/query/id/{orderId}") @ResponseBody
    @Deprecated
    public String getOrderById(@PathVariable("orderId") Long id) {
        Order order = orderService.findOrderById(id);
        return JSON.toJSONStringWithDateFormat(ResponseUtil.responseQueryOne(OrderUtil.getOrderParamMap(order)),
                "yyyy-MM-dd",
                SerializerFeature.WriteDateUseDateFormat);
    }

    @ApiOperation(value = "查询一个用户所有的订单", httpMethod = "GET", response = String.class,
            notes = "返回json，已经删除的订单不再显示")
    @GetMapping("/order/query/list/userId/{userId}") @ResponseBody
    public String queryOrderByUserId(@PathVariable("userId") Long id) {
        List<Map<String, Object>> list = new ArrayList<>();
        List<Order> orderList = orderService.findUserOrders(id);
        for (Order order : orderList) {
            list.add(OrderUtil.getOrderParamMap(order));
        }
        return JSON.toJSONStringWithDateFormat(ResponseUtil.responseQueryList(list),
                "yyyy-MM-dd",
                SerializerFeature.WriteDateUseDateFormat);
    }

    @ApiOperation(value = "取消指定订单id的订单", httpMethod = "GET", response = String.class,
            notes = "返回json")
    @GetMapping("/order/cancel/{orderId}") @ResponseBody
    public String cancelOrder(@PathVariable("orderId") Long id) {
        boolean status = orderService.cancelOrder(id);
        return JSON.toJSONString(ResponseUtil.responseOperation(status));
    }

    @ApiOperation(value = "删除指定订单id的订单", httpMethod = "GET", response = String.class,
            notes = "返回json")
    @GetMapping("/order/delete/{orderId}") @ResponseBody
    public String deleteOrderById(@PathVariable("orderId") Long id) {
        boolean status = orderService.deleteOrder(id);
        return JSON.toJSONString(ResponseUtil.responseOperation(status));
    }

    @ApiOperation(value = "支付指定订单id的订单", httpMethod = "GET", response = String.class,
            notes = "返回json")
    @GetMapping("/order/pay/{orderId}") @ResponseBody
    public String payOrderById(@PathVariable("orderId") Long id) {
        boolean status = orderService.payOrder(id);
        return JSON.toJSONString(ResponseUtil.responseOperation(status));
    }

    @ApiOperation(value = "提交新订单", httpMethod = "POST", response = String.class,
            notes = "data的值是order的json字符串")
    @PostMapping("/order/post") @ResponseBody
    public String postOrder(@RequestBody String data) {
        Map<String, Object> response = new HashMap<>();
        response.put("status", "failed");
        try {
            Order order = JSON.parseObject(data, Order.class);
            String result = orderService.postOrder(order);
            if (result == null) {
                response.put("status", "success");
            } else {
                response.put("info", result);
            }
        } catch (Exception e) {
            response.put("info", "消息格式不正确");
        }
        return JSON.toJSONString(response);
    }

}
